home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-04 | 9.6 KB | 324 lines |
-
- package sub_arctic.test;
-
- import sub_arctic.lib.*;
- import sub_arctic.constraints.std_function;
- import sub_arctic.constraints.constraint;
- import sub_arctic.input.callback_object;
- import sub_arctic.input.event;
- import sub_arctic.output.drawable;
-
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.FontMetrics;
-
- /**
- * Applet to do a demo of various common constraints.
- * @author Scott Hudson
- */
- public class constraint_patterns extends debug_interactor_applet
- implements callback_object, cycle_handler {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- labeled_object parent_obj, prev_sibling, obj, next_sibling;
- label message1, message2, message3;
- text_toggle_collection x_select, w_select;
- scale a_slider;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Initialization of sub_arctic interface when applet starts */
- public void build_ui(base_parent_interactor top)
- {
- Font lab_font = new Font("Helvetica",Font.BOLD,12);
-
- /* build overall column */
- column ui_col = new column(2,column.LEFT_JUSTIFIED);
- ui_col.set_pos(10,10);
- top.add_child(ui_col);
-
- /* Put a title label in */
- ui_col.add_child(new label("A Selection of Commonly Used Constraints",
- new Font("Helvetica", Font.BOLD, 16)));
-
- /* build our sample objects */
- parent_obj = new labeled_object(350,175,"parent_obj");
- prev_sibling = new labeled_object(100,100,"prev_sibling");
- next_sibling = new labeled_object(60,100,"next_sibling");
- obj = new labeled_object(100,100,"obj");
- a_slider = new scale(5,0, 250, 10,200, 100, 10, null);
-
- /* put them in our test hierarchy */
- parent_obj.add_child(prev_sibling);
- prev_sibling.set_x(10);
- prev_sibling.set_y(20);
- parent_obj.add_child(obj);
- obj.set_x(75);
- obj.set_y(40);
- parent_obj.add_child(next_sibling);
- next_sibling.set_x(270);
- next_sibling.set_y(20);
- a_slider.set_y_constraint(std_function.far_edge_just(PARENT.Y2(), 5));
- parent_obj.add_child(a_slider);
- ui_col.add_child(parent_obj);
-
- /* build rest of interface around it */
-
- /* messages */
- message1 = new label("No cycles detected", lab_font);
- ui_col.add_child(message1);
-
- /* x constraint selector */
- ui_col.add_child(new label("Set obj.x constraint to:", lab_font));
- x_select = new text_toggle_collection(x_select_data, true, -1, this);
- x_select.nth_toggle(0).set_cur_state(1);
- ui_col.add_child(x_select);
-
- /* w constraint selector */
- ui_col.add_child(new label("Set obj.w constraint to:", lab_font));
- w_select = new text_toggle_collection(w_select_data, true, -1, this);
- w_select.nth_toggle(0).set_cur_state(1);
- ui_col.add_child(w_select);
-
- /* setup to catch cycles in constraints with callback to us */
- manager.handle_cycles_with(manager.EXCEPTION_CUSTOM, this);
-
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- String[] x_select_data = {
- "obj.x <= [No constraint] 75",
- "obj.x <= [Next to sibling] std_function.offset(PREV_SIBLING.X2(), 5)",
- "obj.x <= [Parent left edge] std_function.offset(PARENT.X(), 5)",
- "obj.x <= [Parent right edge] std_function.far_edge_just(PARENT.X2(), 5)",
- "obj.x <= [Centered in parent] std_function.centered(PARENT.W(), 0)"
- };
-
- String[] w_select_data = {
- "obj.w <= [No constraint] 100",
- "obj.w <= [Fill to parent] "+
- "std_function.fill(SELF.X(), PARENT.X2(), 5)",
- "obj.w <= [Fill to next sib] " +
- "std_function.fill(SELF.X(), NEXT_SIBLING.X(), 5)",
- "obj.w <= [Set by slider] " +
- "std_function.eq(OTHER.OBJ(a_slider).PART_A())"
- };
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Handle callbacks. */
- public void callback(interactor from, event evt, int cb_num, Object cb_parm)
- {
- int obj_val;
-
- /* reset the cycle message (will be set in cycle handler if we have one)*/
- message1.set_text("No cycles detected");
-
- if (from instanceof label_toggle)
- {
- /* is this the x constraint selection */
- if (from.parent() == x_select)
- {
- /* extract the toggle number and act on it */
- obj_val=((Integer)from.user_info()).intValue();
- switch (obj_val)
- {
- case 0:
- obj.set_x_constraint(NO_CONSTRAINT);
- obj.set_x(75);
- break;
- case 1:
- obj.set_x_constraint(std_function.offset(PREV_SIBLING.X2(),5));
- break;
- case 2:
- obj.set_x_constraint(std_function.offset(PARENT.X(), 5));
- break;
- case 3:
- obj.set_x_constraint(
- std_function.far_edge_just(PARENT.X2(),5));
- break;
- case 4:
- obj.set_x_constraint(std_function.centered(PARENT.W(), 0));
- break;
- default:
- message1.set_text("UNKNOWN SETTING FOR OBJ.X!!");
- break;
- }
-
- /* hit the width to make sure we expose any cycles */
- obj.mark_w_ood();
- }
- /* is thsi the w constraint selection */
- else if (from.parent() == w_select)
- {
- /* extract the toggle number and act on it */
- obj_val=((Integer)from.user_info()).intValue();
- switch (obj_val)
- {
- case 0:
- obj.set_w_constraint(NO_CONSTRAINT);
- obj.set_w(100);
- break;
- case 1:
- obj.set_w_constraint(
- std_function.fill(SELF.X(), PARENT.X2(), 5));
- break;
- case 2:
- obj.set_w_constraint(
- std_function.fill(SELF.X(), NEXT_SIBLING.X(), 5));
- break;
- case 3:
- obj.set_w_constraint(
- std_function.eq(OTHER.OBJ(a_slider).PART_A()));
- break;
- default:
- message1.set_text("UNKNOWN SETTING FOR OBJ.W!!");
- break;
- }
-
- /* hit x to make sure we expose any cycles */
- obj.mark_x_ood();
- }
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Handle cycle_detection */
- public boolean handle_cycle(interactor in_obj, int part_code)
- {
- /* change the message then continue with evaluation anyway */
- message1.set_text("CYCLE DETECTED!!");
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
-
- /*---------------------------------------------------------------------*/
-
- /**
- * Small interactor class that draws a rectangle for its bounding box along with
- * a small label at the top left.
- * @author Scott Hudson
- */
- class labeled_object extends base_parent_interactor {
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Constructor
- * @param int wid width of the object.
- * @param int hi height of the object.
- * @param String lab text of the display label
- */
- public labeled_object(int wid, int hi, String lab)
- {
- super(0,0,wid,hi);
- set_text(lab);
- set_font(new Font("Helvetica",Font.PLAIN,9));
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Text for the label we display */
- protected String _text = "";
-
- /**
- * Text for the label we display
- * @return String the string we are currently displaying.
- */
- public String text() {return _text;}
-
- /**
- * Set the text for the label we display
- * @param String t the new label text.
- */
- public void set_text(String t)
- {
- if (!_text.equals(t))
- {
- _text = t;
- damage_self();
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
-
- /** Font to display label in */
- protected Font _font = null;
-
- /** Metrics object for current font */
- protected FontMetrics _metrics = null;
-
- /**
- * Font to display label in
- * @return Font the font we are display in.
- */
- public Font font() {return _font;}
-
- /**
- * Set font to display label in
- * @param Font f the new font.
- */
- public void set_font(Font f)
- {
- if (_font != f)
- {
- _font = f;
- _metrics = manager.get_metrics(f);
- damage_self();
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Output routine for the interactor.
- * @param drawable surf the drawing surface we draw on.
- */
- public void draw_self_local(drawable surf)
- {
- int sz_w, sz_h;
-
- /* draw a rectangle at the bounds */
- surf.setColor(Color.black);
- surf.drawRect(0,0, w()-1, h()-1);
-
- /* measure the string and clear behind it */
- sz_w = _metrics.stringWidth(text()) + 4;
- sz_h = _metrics.getHeight() + 2;
- surf.setColor(Color.white);
- surf.fillRect(0,0, sz_w-1,sz_h-1);
-
- /* draw the text tag */
- surf.setColor(Color.black);
- surf.setFont(font());
- surf.drawString(text(), 2, _metrics.getAscent()+1);
- surf.drawRect(0,0, sz_w-1, sz_h-1);
-
- /* let superclass draw any children */
- super.draw_self_local(surf);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-